<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <div class="filter-item">
        <span class="span">项目名称 </span>
        <el-input v-model="listQuery.name" placeholder="请输入项目名称" style="width: 300px" @keyup.enter.native="handleFilter" />
      </div>
      <el-button v-permission="['sys:organization:list']" v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('table.search') }}
      </el-button>
      <el-button v-permission="['sys:organization:add']" plain class="filter-item" style="margin-left: 10px" type="primary" icon="el-icon-edit" @click="handleCreate">{{ $t('table.add') }}</el-button>
      <el-button v-permission="['sys:organization:list']" v-waves class="filter-item" icon="el-icon-refresh" @click="handleRest">{{ $t('table.rest') }}</el-button>
    </div>
    <div style="">
      <el-table default-expand-all v-loading="listLoading" element-loading-text="正在加载，请稍后" :data="list" style="width: 100%" row-key="id" border>
        <el-table-column prop="name" label="项目名称" />
        <el-table-column prop="userName" label="数据库名称" />
        <el-table-column label="项目备注" align="center">
          <template #default="scope">
            <span style="color: sandybrown">{{ scope.row.remarks }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button v-permission="['sys:organization:update']" size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">{{ $t('table.edit') }}</el-button>
            <el-button v-permission="['sys:organization:delete']" size="small" type="text" icon="el-icon-delete" class="delete-text-btn" @click="handleDelete(scope.row)">{{
              $t('table.delete')
            }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <organization-form ref="form" @refreshList="getList" />
  </div>
</template>

<script>
import { fetchOrganizationList, deleteOrganization } from '@/api/sys/organization'
import permission from '@/directive/permission/permission'
import waves from '@/directive/waves'
import OrganizationForm from './organizationForm' // 水波纹指令

export default {
  name: 'OrganizationList',
  components: { OrganizationForm },
  directives: {
    waves,
    permission,
  },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: null,
      listLoading: true,
      listQuery: {
        name: undefined,
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      this.list = []
      fetchOrganizationList(this.listQuery).then((response) => {
        const organizationList = response.data.data
        console.log(organizationList, 'organizationListorganizationList')
        for (var i = 0; i < organizationList.length; i++) {
          this.list.push({
            id: organizationList[i].id,
            name: organizationList[i].name,
            children: organizationList[i].children,
          })
        }
        this.$refs.form.setList(this.list)
        this.listLoading = false
      })
    },
    handleFilter() {
      this.getList()
    },
    handleCreate() {
      this.$refs.form.handleCreate()
    },
    //重置
    handleRest() {
      this.listQuery = {
        page: 1,
        limit: this.$store.getters.defaultPageSize,
      }
      this.getList()
    },
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row.id)
    },
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteOrganization(row.id).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}
.span {
  color: #606266;
  font-weight: 700;
  font-size: 14px;
}
</style>
